package com.yanqu.road.server.cmd.banquet.cross.goodbanquet;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/9 16:55
 */
@Cmd(code = Protocol.S_GOOD_BANQUET_JOIN_FROM_CROSS, desc = "请求百福宴赴宴 跨服返回区服 协议")
public class GoodBanquetJoinFromCrossCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CrossBanquetProto.GoodBanquetJoinRespToGameServerMsg stcMsg = CrossBanquetProto.GoodBanquetJoinRespToGameServerMsg.parseFrom(packet.getBytes());
        CrossBanquetProto.GoodBanquetJoinRespMsg.Builder builder = CrossBanquetProto.GoodBanquetJoinRespMsg.newBuilder();
        builder.setRet(stcMsg.getRet());
        if(stcMsg.getRet() == 0){
            int count = 0;
            long popularity = 0;
            long totalIntegral = 0;
            Property reward = new Property();
            for (CrossBanquetProto.GoodBanquetJoinUserEntity joinUserEntity : stcMsg.getJoinUserListList()){
                CrossBanquetProto.GoodBanquetJoinEntity.Builder joinEntity = CrossBanquetProto.GoodBanquetJoinEntity.newBuilder();
                // 赴该玩家的宴会数量
                int num = 0;
                // 赴该玩家的宴会所获得的积分
                int integral = 0;
                for (CrossBanquetProto.GoodBanquetJoinListEntity item : joinUserEntity.getJoinListList()){
                    BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(item.getType());
                    BanquetGiftInfo banquetGiftInfo = null;
                    for (BanquetGiftInfo giftInfo : BanquetConfigMgr.getBanquetGiftInfoMap().values()){
                        if(banquetInfo.getType1() == giftInfo.getType1() && banquetInfo.getType2() == giftInfo.getType2()){
                            banquetGiftInfo = giftInfo;
                            break;
                        }
                    }
                    if(banquetGiftInfo == null){
                        CrossBanquetMgr.getLogger().error("百福宴赴宴了却没有奖励配置。type:{}",item.getType());
                        continue;
                    }
                    // 参与获得宴会积分人气
                    if(!"0".equals(banquetGiftInfo.getJoinScore())){
                        Property joinScore = PropertyHelper.parseStringToProperty(banquetGiftInfo.getJoinScore());
                        popularity += (joinScore.getCountByGoodsId(GameConfig.BANQUET_POPULARITY).longValue() * item.getPosListCount());
                        integral += (joinScore.getCountByGoodsId(GameConfig.BANQUET_INTEGRAL).longValue() * item.getPosListCount());
                    }
                    // 参与奖励
                    if(!"0".equals(banquetGiftInfo.getJoinReward())){
                        for (Integer pos : item.getPosListList()){
                            reward.addProperty(PropertyHelper.parseStringToProperty(BanquetConfigMgr.getJoinReward(pos,banquetGiftInfo.getId())));
                        }
                    }
                    num += item.getPosListCount();
                }
                count += num;
                totalIntegral += integral;

                joinEntity.setNum(num);
                joinEntity.setIntegral(integral);
                joinEntity.setName(joinUserEntity.getName());
                builder.addJoinList(joinEntity);
            }
            if(count > 0){
                //加一下参加宴会次数
                CrossBanquetMgr.addBanquetPopularityAndIntegral(player,popularity,totalIntegral,eLogMoneyType.BanquetJoinReward.getValue());

                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Banquet, eLogMoneyType.BanquetJoinReward);
                reward.addProperty(GameConfig.BANQUET_INTEGRAL,BigInteger.valueOf(popularity));

                builder.setRewards(PropertyHelper.parsePropertyToString(reward));
                builder.setNum((int) popularity);
                BanquetMgr.addBanquetJoinTimes(player,count);
            }
        }
        player.sendPacket(ClientProtocol.U_CROSS_GOOD_BANQUET_JOIN, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
